﻿using System;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;

using Sunyi.Project.WebPoint.Common;
using Sunyi.Project.WebPoint.DataAccess;

using ActivitySystem.DataContract;

namespace ActivitySystem.DataAccess
{
    public partial class ParticipationDao
    {
        public Collection<Participation> SelectByUserIdAndActivityId(Guid userId, Guid activityId)
        {
            Collection<Participation> participations = new Collection<Participation>();

            SqlConnection connection = ConnectionProvider.CreateConnection();
            SqlCommand command = connection.CreateCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "procTR_Participation_SelectByUserIdAndActivityId";
            command.Parameters.Add(new SqlParameter("@UserId", userId));
            command.Parameters.Add(new SqlParameter("@ActivityId", activityId));

            try
            {
                connection.Open();
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        participations.Add(CreateEntityByReader(reader));
                    }
                }
            }
            finally
            {
                ConnectionProvider.CloseConnection(connection);
            }

            return participations;
        }
    }
}
